home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-29 | 4.9 KB | 193 lines | [TEXT/MPS ] |
- {$R-}
- {$S ClipBoardToPictFile }
-
- {
-
- ClipBoardToPictFile(FileName, CreatorType)
-
- This HyperCard XFCN takes the PICT on the clipboard and turns it into
- a PICT file with the given name. It does not matter whether the PICT came
- from the HyperCard paint tools, from another XCMD, or from another
- application altogether.
-
- The optional parameter CreatorType is a four-character string
- which will be the creator type of the file. This will allow the file
- to be double-clicked to invoke the corresponding application. The
- default value is '????' -- which means no application.
-
- If it is successful, then empty is returned, otherwise the return value
- is an error message.}
-
- UNIT DummyUnit;
-
- INTERFACE
-
- USES {* ToolIntf, PackIntf, *}
- Menus, Events, TextEdit, HyperXCmd,
- OSIntf, Scrap, QuickDraw;
-
- PROCEDURE EntryPoint(paramPtr: XCmdPtr);
-
- IMPLEMENTATION
-
- PROCEDURE ClipBoardToPictFile(paramPtr: XCmdPtr);
- FORWARD;
-
- PROCEDURE EntryPoint(paramPtr: XCmdPtr);
- BEGIN
- ClipBoardToPictFile(paramPtr)
- END { entrypoint } ;
-
-
- PROCEDURE ClipBoardToPictFile(paramPtr: XCmdPtr);
-
- CONST
-
- MinParams = 1;
- MaxParams = 2;
-
- PictHeaderSize = 512;
- XFCNSignature = 'Created by ClipBoardToPictFile XFCN by Chris Thorman';
-
- TYPE
-
- ParamArray = PACKED ARRAY [1..MaxParams] OF Str255;
-
- PictFileHeader = PACKED ARRAY [1..PictHeaderSize] OF CHAR;
-
-
- VAR
-
- ParamStrings: ParamArray;
-
- FileNameParam: Str255;
- CreatorTypeParam: OSType;
-
- OutFileRefNum: Integer;
-
- ThePict: Handle;
- PictSize: LONGINT;
- ScrapOffset: LONGINT;
- FileError: Integer;
- Success: Boolean;
-
- PROCEDURE ExitWithMessage(aString: Str255);
- BEGIN
- WITH paramPtr^ DO BEGIN
- returnValue := PasToZero(paramPtr, aString);
- EXIT(ClipBoardToPictFile);
- END;
- END;
-
- PROCEDURE ExitWithError(aString: Str255);
- BEGIN
- ExitWithMessage(concat('•••••••• Error: ', aString, '.'));
- END;
-
- FUNCTION WritePICTFile(ThePict: Handle; OutFileRefNum: Integer): Boolean;
- VAR
- Success: Boolean;
- FileHeader: PictFileHeader;
- NumBytes: LONGINT;
- Signature: Str255;
-
- BEGIN
-
- {* The file header contains nothing but a gratuitous signature *}
- Signature := XFCNSignature;
-
- BlockMove(Ptr(ORD(@Signature) + 1), @FileHeader, length(Signature));
-
- NumBytes := PictHeaderSize;
- Success := (FSWrite(OutFileRefNum, NumBytes, @FileHeader) = NoErr);
- WritePICTFile := Success;
- IF (NOT Success) THEN Exit(WritePICTFile);
-
- {* The rest of the file is just the PICT itself *}
- NumBytes := GetHandleSize(ThePict);
- Success := (FSWrite(OutFileRefNum, NumBytes, ThePict^) = NoErr);
- WritePICTFile := Success;
- IF (NOT Success) THEN Exit(WritePICTFile);
-
- END;
-
- PROCEDURE ParseParams;
- VAR
- ParamNum: integer;
- BEGIN
- WITH paramPtr^ DO
- BEGIN
- IF (paramCount < MinParams) THEN ExitWithError('Too few parameters');
- IF (paramCount > MaxParams) THEN ExitWithError('Too many parameters');
-
- ParamNum := 1; {* Required *}
-
- ZeroToPas(ParamPtr, Params[ParamNum]^, ParamStrings[ParamNum]);
- FileNameParam := ParamStrings[ParamNum];
- IF (FileNameParam = '') THEN ExitWithError('Empty file name');
-
- ParamNum := 2; {* Optional *}
-
- IF (paramCount >= ParamNum) THEN
- BEGIN
- ZeroToPas(paramPtr, params[ParamNum]^, ParamStrings[ParamNum]);
- IF (length(ParamStrings[ParamNum]) <> 4)
- THEN ExitWithError(concat('Bad Creator Type: ', ParamStrings[ParamNum]));
-
- CreatorTypeParam[1] := ParamStrings[ParamNum][1];
- CreatorTypeParam[2] := ParamStrings[ParamNum][2];
- CreatorTypeParam[3] := ParamStrings[ParamNum][3];
- CreatorTypeParam[4] := ParamStrings[ParamNum][4];
- END
- ELSE
- BEGIN
- CreatorTypeParam := '????';
- END;
- END;
- END;
-
- BEGIN {ClipBoardToPictFile}
-
- ParseParams;
-
- ThePict := NewHandle(0);
- IF (ThePict = NIL) THEN ExitWithError('Couldn’t allocate zero-size handle');
-
- PictSize := GetScrap(ThePict, 'PICT', ScrapOffset);
- IF (PictSize = 0)
- THEN
- BEGIN
- DisposHandle(ThePict);
- ExitWithError('Pict was of zero size');
- END;
- IF (PictSize < 0)
- THEN
- BEGIN
- DisposHandle(ThePict);
- IF (PictSize = NoTypeErr) THEN ExitWithError('No data of type PICT on clipboard');
- ExitWithError('Unknown error in getting PICT');
- END;
-
- IF (Create(FileNameParam, 0, CreatorTypeParam, 'PICT') <> NoErr)
- THEN ExitWithError(concat('Couldn’t create file: ', FileNameParam, '. It may already exist'));
-
- IF (FSOpen(FileNameParam, 0, OutFileRefNum) <> NoErr)
- THEN ExitWithError(concat('Couldn’t open file: ', FileNameParam));
-
- Success := WritePICTFile(ThePict, OutFileRefNum);
-
- DisposHandle(ThePict);
- FileError := FSClose(OutFileRefNum);
-
- IF (NOT Success) THEN ExitWithError(concat('Error Writing PICT to File: ', FileNameParam));
- IF (FileError <> NoErr) THEN ExitWithError('Error closing file');
-
- ExitWithMessage('');
-
-
- END { ClipBoardToPictFile } ;
-
- END. { DummyUnit }
-
-
-